import java.io.Serializable;

/**
 * Vector Timestamp representation 
 *
 * @author Don Pinto
 */
class VTS implements Serializable, Comparable
{
   /**
    * index of the sender
    */
   int senderPosition;

   /**
    * values represented as an array
    */
   int[] values;

   /**
    * Constructs a Vector timestamp with sender index and vector values
    *
    * @param senderIndex index of the sender of the message
    * @param values vector values
    */
   public VTS(int senderIndex, int numClients)
   {
      this.values = new int[numClients]; // Create an array with numClients
      this.senderPosition = senderIndex;
   }

   /**
    * Returns sender index
    * @return sender index position
    */
   public int getSenderIndex()
   {
      return senderPosition;
   }

   /**
    * Returns vector values
    * @return an array of vector values
    */
   public int[] getValues()
   {
      return values;
   }

   /**
    * Returns size of this vector timestamp  i.e number of process group members
    * @return vector timestamp size
    */
   public int size()
   {
      return values.length;
   }

   
   /**
    *<p>
    *Checks if this VTS is less than equal from the given
    *other VTS follows:
    *</p>
    * <p>
    * VT1<=VT2 iff for every i:1..k VT1[i]<=VT2[i]
    * </p>
    * @param other VTS compared with this.
    * @return true if this VTS less than or equal from
    * other, false othwerwise
    */
   public boolean lessThanOrEqual(VTS other)
   {
      int[] b = other.getValues();
      int[] a = values;
      for (int k = 0; k < a.length; k++)
      {
         if (a[k] <= b[k])
            continue;
         else
            return false;
      }
      return true;
   }

   /**
    * <p>
    * Checks if this VTS equal to given VTS
    * as follows:
    * </p>
    * <p>
    *  VT1==VT2 iff for every i:1..k VT1[i]==VT2[i]
    * @param other VTS compared with this.
    * @return true if the above given equation is true, false otherwise
    */
   public boolean equals(VTS other)
   {
      int a [] = getValues();
      int b [] = other.getValues();

      for (int i = 0; i < a.length; i++)
         if (a[i] != b[i]) return false;

      return true;
   }
   
   
   public int compareTo(Object other)
   {
	   if ((other instanceof VTS) == true)
	   {
		 if(this.equals((VTS)other))
			 return 0;
		 else if(this.lessThanOrEqual((VTS)other))
			 return -1;
		 else
			 return 1;
	   }
	   return -1;
   }
   
   public void increment(int senderPosition){
	   values[senderPosition-1]++;
   }
   
   /**
    * Returns String representation of this vector timestamp
    * @return String representing this vetor timestamp
    */
   public String toString()
   {
      String classType = "VTS[";
      int bufferLength = classType.length() + values.length * 2 + 1;
      StringBuffer buf = new StringBuffer(bufferLength);
      buf.append(classType);
      for (int i = 0; i < values.length - 1; i++)
      {
         buf.append(values[i]).append(',');
      }
      buf.append(values[values.length - 1]);
      buf.append(']');
      return buf.toString();
   }
}

